package com.genius.reservation.common.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.reservation.entity.ReservationInformation;

import java.util.HashMap;
import java.util.Map;

public class BaseMessage {

    public static Map<String, ReservationInformation> message = new HashMap<>();


    public void newMessage(JSONObject obj) {
        synchronized (this) {
            ReservationInformation data = (ReservationInformation) (obj.get("data"));
            message.put(data.getReservationNumber(), data);
        }
    }


    public ResponseRule getMessage(String receiveManagerNumber) {
        synchronized (this) {
            ResponseRule<ReservationInformation> ok;
            if (message.size() != 0) {
                ok = ResponseRule.ok(message.get(receiveManagerNumber));
                message.remove(receiveManagerNumber);
            } else {
                ok = ResponseRule.ok("没有新消息");
            }
            return ok;
        }
    }

    public ResponseRule getAllMessage() {
        synchronized (this) {
            ResponseRule<Map<String, ReservationInformation>> ok;
            if (message.size() != 0) {
                ok = ResponseRule.ok(message);
                message.clear();
            } else {
                ok = ResponseRule.ok("没有新消息");
            }
            return ok;
        }

    }
}
